02 / 10

What are hooks in Cypress, and how are they used?

In Cypress, Hooks are special functions borrowed from the Mocha testing framework that allow you to run a set of commands before or after your tests.

  1. 1

    before(): Executes once before all tests in a block. Used in Global setup (e.g. login, load data)

  2. 2

    after(): Executes once after all tests in a block. Used in Global cleanup (e.g. logout, clear data)

  3. 3

    beforeEach(): Executes before each test in a block. Used in Reset state, visit URL before each test

  4. 4

    afterEach(): Executes after each test in a block. Used in Clean up per test (e.g. delete test data)

javascript